<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Url;
use think\Session;
use think\Controller;
use app\index\model\Order as thisModel;

/**
 * 
 */
class Order extends Base
{
	// 订单列表
	public function index()
	{
		$status = input('param.status');
		if(!empty($status))
		{
			// var_dump($status).die();
			switch ($status) {
				case 'cai':/*带财审*/
					$where['status'] = 'caishen';
					break;
				case 'confirm':/*待审核*/
					$where['status'] = 'confirm';
					break;
				case 'ding':/*待录成衣数据*/
					$where['status'] = 'input';
					break;
				case 'shang':/*已录入 待上传*/
					$where['status'] = 'cheng';
					break;
				case 'xia'://已上传  待下载
					$where['status'] = 'upload';
					break;
				case 'fa'://已下载，待发货
					$where['status'] = 'download';
					break;
				case 'yijing'://已发货
					$where['status'] = 'success';
					break;
				case 'bo'://驳回
					$where['status'] = 'reject';
					break;	
				case 'zheng'://正常订单
					$where['shuxing'] = 1;
					break;
				case 'shou'://售后订单
					$where['shuxing'] = 2;
					break;
				case 'dai'://代加工订单
					$where['is_dai'] = 1;
					break;			
			}
		}
		$authmenu = new \think\Authmenu();
		$where['is_del'] = 0;
		$uid = Session::get('manage_id');
		$shop_id = DB::name('user')->where('id ='.$uid)->value('shop_id');
		$auth=$authmenu->check($uid,'order_shop');
		if($auth == true){
			$where['shop_id'] = $shop_id;
		}
		$all = $authmenu->check($uid,'order_person');
		if($all == true){
			$where['user_id'] = Session::get('manage_id');
		}
		if(request::instance()->post()){
			$name = request::instance()->post('name');
			if(strlen($name)>10){
				$custid = DB::name('customer')->field('id')->where("status=1 and is_del =0 and phone like '%".$name."%'")->select();
			}else{
				$custid = DB::name('customer')->field('id')->where("status=1 and is_del =0 and name like '%".$name."%'")->select(); 
			}
			$cust = '';
			foreach ($custid as $k => $v) {
				$cust .= $v['id'].','; 
			}
			$cust = substr($cust,0,-1);
			$where['cust_id'] =array('in',$cust);
		}
		$list = DB::name('order')->where($where)->order('id desc')->paginate(15);
		$page = $list->render();
		$this->assign('page', $page);
		$list = $list->all();
		foreach ($list as $k => $v) {
			$cust = DB::name('customer')->where('id = '.$v['cust_id'])->find();
			$list[$k]['cust_name'] = $cust['name'];
			$list[$k]['cust_phone'] = $cust['phone'];
			$list[$k]['pin'] = DB::name('category')->where('id = '.$v['category_id'])->value('title');
			$dizhi = $this->geAddress($cust['province'],$cust['city'],$cust['area']);
			$list[$k]['dizhi'] = $dizhi . $cust['address'];
			$add = $authmenu->check($uid,'order_add');
			switch ($v['status']) {
				case 'caishen'://等待财务审核
				if($add == true){
					$list[$k]['cao'] = '<a href="/index/order/add/id/'.$v['first_id'].'" title="添加"><i class="iconfont icon-tianjiaadd142"></i></a>&emsp;<a href="/index/order/cai/id/'.$v['id'].'" title="待财审"><i class="iconfont icon-daishenhe"></i></a>&emsp;<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
				}else{
					$list[$k]['cao'] = '<a href="/index/order/cai/id/'.$v['id'].'" title="审核"><i class="iconfont icon-daishenhe"></i></a>&emsp;<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
				}
				$list[$k]['status'] = '<a data-toggle="modal"  href="/index/order/cai/id/'.$v['id'].'" ><span class="badge badge">待财审</span></a>';
				break;
				case 'confirm'://等待审核
				if($add == true){
					$list[$k]['cao'] = '<a href="/index/order/add/id/'.$v['first_id'].'" title="添加"><i class="iconfont icon-tianjiaadd142"></i></a>&emsp;<a href="/index/order/confirm/id/'.$v['id'].'" title="审核"><i class="iconfont icon-daishenhe"></i></a>&emsp;<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
				}else{
					$list[$k]['cao'] = '<a href="/index/order/confirm/id/'.$v['id'].'" title="审核"><i class="iconfont icon-daishenhe"></i></a>&emsp;<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
				}
				$list[$k]['status'] = '<a data-toggle="modal"  href="/index/order/confirm/id/'.$v['id'].'" ><span class="badge badge-warning">待审核</span></a>';
				break;
				case 'reject'://审核驳回
				$list[$k]['status'] = '<a data-toggle="modal"  href="/index/order/updatejing/id/'.$v['id'].'" ><span class="badge badge-danger">已驳回</span></a>';
				if($add == true){
					$list[$k]['cao'] = '<a href="/index/order/add/id/'.$v['first_id'].'" title="添加"><i class="iconfont icon-tianjiaadd142"></i></a>&emsp;<a href="/index/order/updatejing/id/'.$v['id'].'" title="修改"><i class="iconfont icon-chakan1"></i></a>&emsp;<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
				}else{
					$list[$k]['cao'] = '<a href="/index/order/updatejing/id/'.$v['id'].'" title="修改"><i class="iconfont icon-chakan1"></i></a>&emsp;<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';					
				}
				break;
				case 'input':
				if($add == true){
					$list[$k]['cao'] = '<a href="/index/order/add/id/'.$v['first_id'].'" title="添加"><i class="iconfont icon-tianjiaadd142"></i></a>&emsp;<a href="/index/order/update/id/'.$v['id'].'" title="录入"><i class="iconfont icon-luru1"></i></a>&emsp;<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
				}else{
					$list[$k]['cao'] = '<a href="/index/order/update/id/'.$v['id'].'" title="录入"><i class="iconfont icon-luru1></i></a>&emsp;<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
				}
				$list[$k]['status'] = '<a data-toggle="modal"  href="/index/order/update/id/'.$v['id'].'" ><span class="badge badge-danger">待定版</span></a>';
				break;
				case 'cheng'://成衣数据已录入，等待上传压缩包
				$shang = $authmenu->check($uid,'order_upload');
				if($shang == true){

					$list[$k]['status'] = '<a data-toggle="modal"  href="#modal-form" onclick="getInfo('.$v['id'].')" title="上传"><span class="badge badge-info">待上传</span></a>';
					if($add == true){
						$list[$k]['cao'] = '<a href="/index/order/add/id/'.$v['first_id'].'" title="添加"><i class="iconfont icon-tianjiaadd142"></i></a>&emsp;<a data-toggle="modal"  href="#modal-form" onclick="getInfo('.$v['id'].')" title="上传"><i class="iconfont icon-shangchuan"></i></a>&emsp;<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';	

					}else{
						$list[$k]['cao'] = '<a data-toggle="modal"  href="#modal-form" onclick="getInfo('.$v['id'].')" title="上传"><i class="iconfont icon-shangchuan"></i></a>&emsp;<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
					}
				}else{
					$list[$k]['status'] = '<a data-toggle="modal"  href="#"  title="上传"><span class="badge badge-info">待上传</span></a>';
					if($add == true){
						$list[$k]['cao'] = '<a href="/index/order/add/id/'.$v['first_id'].'" title="添加"><i class="iconfont icon-tianjiaadd142"></i></a>&emsp;<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
					}else{
						$list[$k]['cao'] = '<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
					}
				}
				break;	
				case 'upload'://压缩包已上传，等待下载
				$list[$k]['status'] = '<a data-toggle="modal" href="JavaScript:;" onclick="xiazai('.$v['id'].')"><span class="badge badge-primary">待下载</span></a>';
				if($add == true){
					$list[$k]['cao'] = '<a href="/index/order/add/id/'.$v['first_id'].'" title="添加"><i class="iconfont icon-tianjiaadd142"></i></a>&emsp;<a href="/index/order/printOrder/id/'.$v['id'].'" title="打印"><i class="iconfont icon-dayin"></i></a>&emsp;<a href="JavaScript:;" onclick="xiazai('.$v['id'].')" title="下载"><i class="iconfont icon-xiazai"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
				}else{
					$list[$k]['cao'] = '<a href="/index/order/printOrder/id/'.$v['id'].'" title="打印"><i class="iconfont icon-dayin"></i></a>&emsp;<a href="JavaScript:;" onclick="xiazai('.$v['id'].')" title="下载"><i class="iconfont icon-xiazai"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
				}
				break;
				case 'download'://已下载
				$fa = $authmenu->check($uid,'order_fahuo');
				if($fa == true){
					$list[$k]['status'] = '<a data-toggle="modal"  href="#modal-form_yundan" onclick="getInfo('.$v['id'].')" title="发货"><span class="badge badge-warning">待发货</span></a>';
					if($add == true){
						$list[$k]['cao'] = '<a href="/index/order/add/id/'.$v['first_id'].'" title="添加"><i class="iconfont icon-tianjiaadd142"></i></a>&emsp;<a data-toggle="modal"  href="#modal-form_yundan" onclick="getInfo('.$v['id'].')" title="发货"><i class="iconfont icon-fahuo"></i></a>&emsp;<a href="/index/order/printOrder/id/'.$v['id'].'" title="打印"><i class="iconfont icon-dayin"></i></a>&emsp;<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
					}else{
						$list[$k]['cao'] = '<a href="/index/order/printOrder/id/'.$v['id'].'" title="打印"><i class="iconfont icon-dayin"></i></a>&emsp;<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
					}
				}else{
					$list[$k]['status'] = '<a data-toggle="modal"  href="JavaScript:;" ><span class="badge badge-success">待发货</span></a>';
					if($add == true){
						$list[$k]['cao'] = '<a href="/index/order/add/id/'.$v['first_id'].'" title="添加"><i class="iconfont icon-tianjiaadd142"></i></a>&emsp;<a href="/index/order/printOrder/id/'.$v['id'].'" title="打印"><i class="iconfont icon-dayin"></i></a>&emsp;<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
					}else{
						$list[$k]['cao'] = '<a href="/index/order/printOrder/id/'.$v['id'].'" title="打印"><i class="iconfont icon-dayin"></i></a>&emsp;<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
					}
				}
				break;
				case 'success':
				$list[$k]['status'] = '<a data-toggle="modal"  href="JavaScript:;" ><span class="badge badge-success">已发货</span></a>';
				if($add == true){
					$list[$k]['cao'] = '<a href="/index/order/add/id/'.$v['first_id'].'" title="添加"><i class="iconfont icon-tianjiaadd142"></i></a>&emsp;<a href="/index/order/printOrder/id/'.$v['id'].'" title="打印"><i class="iconfont icon-dayin"></i></a>&emsp;<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
				}else{
					$list[$k]['cao'] = '<a href="/index/order/printOrder/id/'.$v['id'].'" title="打印"><i class="iconfont icon-dayin"></i></a>&emsp;<a href="/index/order/show/id/'.$v['id'].'" title="查看"><i class="iconfont icon-chakan"></i></a>&emsp;<a href="javascript:;" title="删除" onclick="delorder('.$v['id'].')"><i class="iconfont icon-shanchu"></i></a>';
				}
				break;
				default:
					# code...
				break;
			}
		}
		return $this->fetch('index',[
			'list'  => $list,
		]);
	}
	/*创建订单*/
	public function create()
	{
		$authmenu = new \think\Authmenu();//先判断有没有添加的权限
		$uid = Session::get('manage_id');
		$auth=$authmenu->check($uid,'order_add');
		if($auth != true){
			$this->error('权限不足');
			exit();
		}
		if(Request::instance()->isPost()){
			
			
			$cust = Request::instance()->post('cust/a');
			$jing = Request::instance()->post('jing/a');
			$order = Request::instance()->post('order/a');
			$picarr = Request::instance()->post('picarr/a');
						/*序列化组图*/
			if(!empty($picarr)&&is_array($picarr))
			{
				$info = input('info/a');
				$show = input('show/a');

				$arr = array();
				foreach($picarr as $key=>$v){
					$row['img']  = $v;
					$row['info'] = $info[$key];
					$row['show'] = $show[$key];
					$arr[]=$row;
				}
				$jing['picarr'] =  serialize($arr); 
			}
			DB::startTrans();
			try{
				$cust['shop_id'] = $jing['shop_id'] = $order['shop_id'] = Request::instance()->post('shop_id');
				$cust['user_id'] = $jing['user_id'] = $order['user_id'] = Request::instance()->post('user_id');
				if($cust['user_id'] == -1 || $jing['user_id']==-1 || $cust['from'] == 0 ){
					$this->error('信息不完整');
					exit();
				}
				/*判断会员卡剩余金额是否够本次消费*/
				if($order['payment'] == 0 && $order['money'] < $order['true_price'])
				{
					$this->error('会员卡余额不足，请先去充值');
					exit();
				}
				$cust['status']	= $jin['status'] = 1;
				$cust['is_del'] = $jing['is_del'] = $order['is_del'] = 0;
				$cust['create_time']  = $jing['add_time'] = $jing['update_time'] = $order['add_time'] = time();
				$customer = DB::name('customer')->insertGetId($cust);
				if($customer >0){//说明用户注册成功了
					$jing['cust_id'] = $customer;
					$jing = DB::name('order_first')->insertGetId($jing);
					if($jing >0){//净体数据已经录入完成
						$order['cust_id'] = $customer;
						$order['author'] = Session::get('manage_id');
						$order['first_id'] = $jing;
						$order['status'] = 'caishen';
						$order['order_num'] = time().rand(10000,99999);
						unset($order['money']);
						$createOrder = DB::name('order')->insert($order);
						if($createOrder){//订单生成
							/*判断要是用了会员卡 就进行会员卡的操作*/
							if($order['payment'] == 0 ){
								/*先进行定制消费记录的插入*/
								$online['card_num']		=	$order['card_num'];
								$online['order_num']	=	$order['order_num'];
								$online['category']		=	$order['category_id'];
								$online['entry_id']		=	$order['user_id'];
								$online['shop_id']		=	$order['shop_id'];
								$online['money']		=	$order['true_price'];
								$online['add_time']		=	time();
								$member_online	=	DB::name('member_online')->insert($online);
								$member 		=	DB::name('member')->where('card_num = '.$online['card_num'])->setDec('money',$online['money']);
							}
							/*发送邮件  entry：录入者  time：下单时间 mailcontent:邮件内容 */
							$entry = DB::name('user')->where('id = '.Session::get('manage_id'))->value('truename');
							$time = date("Y-m-d H:i:s",$order['add_time']);
							$mailcontent = $entry."在".$time."为客户".$cust['name']."下单，订单号为：".$order['order_num']."。请赶快前去系统上进行财务审核吧";//邮件内容
							sendMail('3379444781@qq.com','消息提示',$mailcontent);
						}
					}
					//$this->success('用户信息保存成功');
				}
				Db::commit();
			}catch (\Exception $e)
			{
				Db::rollback();$this->error('订单录入失败');
			}
			$this->success('订单录入成功',url('index/index/index',array('con'=>'order','act'=>'index')));
		}
		$prov = $this->appendarg();
		$shop = $this->getShopList();
		$level = $this->getLevel();
		$category = $this->getCategory();
		$style = $this->getStyle();
		return $this->fetch('create',[
			'prov'		=>	$prov,
			'shop'		=>	$shop,
			'level'		=>	$level,
			'category'	=>	$category,
			'style'		=>	$style,
		]);
	}

	/*添加第二个订单*/
	public function add($id)
	{
		$data = DB::name('order_first')->field('cust_id,id,shop_id,user_id')->where('id = '.$id)->find();
		$data['customer'] = $this->getCustomer($data['cust_id']);
		if(request::instance()->isPost())
		{
			$order = request::instance()->post('order/a');
			$order['cust_id'] = $data['cust_id'];
			$order['author'] = Session::get('manage_id');
			$order['first_id'] = $id;
			$order['status'] = 'caishen';
			$order['is_del'] = 0;
			$order['shop_id'] = $data['shop_id'];
			$order['user_id'] = $data['user_id'];
			$order['add_time'] = $order['update_time'] = time();
			$order['order_num'] = time().rand(10000,99999);
			if($order['payment'] == 0 && $order['true_price'] >$order['money'])
			{
				$this->error('会员卡余额不足，请先去充值');
				exit();
			}
			DB::startTrans();
			try
			{	/*添加订单*/
				unset($order['money']);
				$res = DB::name('order')->insert($order);
				/*添加定制消费记录*/
				if($order['payment'] ==	0){
					$online['card_num']		=	$order['card_num'];
					$online['order_num']	=	$order['order_num'];
					$online['category']		=	$order['category_id'];
					$online['money']		=	$order['true_price'];
					$online['entry_id']		=	$order['user_id'];
					$online['shop_id']		=	$order['shop_id'];
					$online['add_time']		=	time();
					$member_online		=	DB::name('member_online')->insert($online);
					$member				=	DB::name('member')->where('is_del = 0 and card_num ='.$order['card_num'])->setDec('money',$order['true_price']);
				}
				DB::commit();
			}catch (\Exception $e)
			{
				DB::rollback();
				$this->error('添加失败');
			}
			$this->success('添加成功',url('index/index/index',array('con'=>'order','act'=>'index')));
		}
		$category = $this->getCategory();
		$style = $this->getStyle();
		return $this->fetch('add',[
			'category'	=>	$category,
			'data'		=>	$data,
			'style'		=>	$style,
		]);
	}


	/*添加成衣尺寸*/
	public function update($id)
	{
	    //全局权限
		$authmenu = new \think\Authmenu();
		$uid = Session::get('manage_id');
		$auth=$authmenu->check($uid,'order_cheng');
		if($auth != true){
			$this->error('权限不足');
			exit();
		}
		if(request::instance()->isPost())
		{
			$data = request::instance()->post('cheng/a');
			$data['status'] = 1;
			$data['is_del'] = 0 ;
			$data['update_time'] = $data['add_time'] = time();
			$res = DB::name('order_second')->insertGetId($data);
			if($res >0)
			{
				$id = request::instance()->post('id');
				$save['cheng_id'] =  $uid;
				$save['second_id'] = $res;
				$save['cheng_time'] =  time();
				$save['status'] = 'cheng';
				$order = DB::name('order')->where('id = '.$id)->setField($save);
				if($order)
				{	
					$this->success('成衣数据添加成功',url('index/index/index',array('con'=>'order','act'=>'index')));
				}
			}
		}
		$data = DB::name('order')->where('id = '.$id)->find();
		$data['first'] = DB::name('order_first')->where('id ='.$data['first_id'])->find();
		$data['picarr'] = unserialize($data['first']['picarr']);
		$data['customer'] = $this->getCustomer($data['cust_id']);
		$data['yifu_style'] = $this->getStyleName($data['yifu_style']);
		$category = $this->getCategory();
		return $this->fetch('update',[
			'data'		=>	$data,
			'category'	=>	$category,
		]);
	}

	/*打印页面  打印页面只有打印功能 没有修改功能*/
	public function printOrder($id)
	{
		$authmenu = new \think\Authmenu();
		$uid = Session::get('manage_id');
		$auth=$authmenu->check($uid,'order_print');
		if($auth != true){
			$this->error('权限不足');
			exit();
		}
		$data = DB::name('order')->where('id = '.$id)->find();

		$data['first'] = DB::name('order_first')->where('id ='.$data['first_id'])->find();
		if(!empty($data['second_id'])){
		$data['cheng'] = DB::name('order_second')->where('id = '.$data['second_id'])->find();
		}else{
			$data['cheng'] = 0;
		}
		$data['customer'] = $this->getCustomer($data['cust_id']);
		$data['yifu_style'] = $this->getStyleName($data['yifu_style']);
		$category = $this->getCategory();
		return $this->fetch('print',[
			'data'		=>	$data,
			'category'	=>	$category,
		]);
	}

	public function updatejing($id)
	{
		// 先判断下有没有修改的权限
		$authmenu = new \think\Authmenu();
		$uid = Session::get('manage_id');
		$auth=$authmenu->check($uid,'order_update');
		if($auth != true){
			$this->error('权限不足');
			exit();
		}

		$data = DB::name('order')->where('id = '.$id)->find();
		if(request::instance()->isPost())
		{
			$id = request::instance()->post('id');
			$jing = request::instance()->post('jing/a');
			$order = request::instance()->post('order/a');
			$picarr = Request::instance()->post('picarr/a');
						/*序列化组图*/
			if(!empty($picarr)&&is_array($picarr))
			{
				$info = input('info/a');
				$show = input('show/a');

				$arr = array();
				foreach($picarr as $key=>$v){
					$row['img']  = $v;
					$row['info'] = $info[$key];
					$row['show'] = $show[$key];
					$arr[]=$row;
				}
				$jing['picarr'] =  serialize($arr); 
			}

			$jing_id = $jing['id'];
			unset($jing['id']);
			$jing['update_time'] = time();
			$first = DB::name('order_first')->where('id = '.$jing_id)->setField($jing);
			if($first){
				$order['update_time'] = time();
				if(empty($data['check_id']) && empty($data['check_time'])){
					$order['status'] = 'caishen';
				}else{
					$order['status'] = 'confirm';
				}
				$res = DB::name('order')->where('id = '.$id)->setField($order);
				if($res)
				{
					$this->success('修改信息成功',url('index/index/index',array('con'=>'order','act'=>'index')));
				}
			}
		}
		$data['first'] = DB::name('order_first')->where('id ='.$data['first_id'])->find();
		$data['picarr'] = unserialize($data['first']['picarr']);
		$data['customer'] = $this->getCustomer($data['cust_id']);
		$category = $this->getCategory();
		$style = $this->getStyle();
		return $this->fetch('updatejing',[
			'data'		=>	$data,
			'category'	=>	$category,
			'style'		=>	$style,
		]);
	}

	/*查看详细信息*/
	public function show($id)
	{
		$data = DB::name('order')->where('id = '.$id)->find();
		$data['first'] = DB::name('order_first')->where('id ='.$data['first_id'])->find();
		$data['picarr'] = unserialize($data['first']['picarr']);
		$data['yifu_style'] = $this->getStyleName($data['yifu_style']);
		if(empty($data['second_id'])){
			$data['cheng'] = array();
		}else{
			$data['cheng'] = DB::name('order_second')->where('id = '.$data['second_id'])->find();
		}
		$data['customer'] = $this->getCustomer($data['cust_id']);
		$data['yonghu'] = DB::name('user')->where('id = '.$data['user_id'])->value('truename');
		
		if(empty($data['caiwu_id'])){
			$data['caiwu_name'] = '暂无';
			$data['caiwu_time'] = '暂无';
		}else{
			$data['caiwu_name'] = DB::name('user')->where('id = '.$data['caiwu_id'])->value('truename');
			$data['caiwu_time'] = date('Y-m-d H:i:s',$data['caiwu_time']);
		}
		if(empty($data['check_id'])){
			$data['check_name'] = '暂无';
			$data['check_time'] = '暂无';
		}else{
			$data['check_name'] = DB::name('user')->where('id = '.$data['check_id'])->value('truename');
			$data['check_time'] = date('Y-m-d H:i:s',$data['check_time']);
		}if(empty($data['upload_id'])){
			$data['upload_name'] = '暂无';
			$data['upload_time'] = '暂无';	
		}else{
			$data['upload_name'] = DB::name('user')->where('id = '.$data['upload_id'])->value('truename');
			$data['upload_time'] = date('Y-m-d H:i:s',$data['upload_time']);
		}if(empty($data['download_id'])){
			$data['download_name'] = '暂无';
			$data['download_time'] = '暂无';
		}else{
			$data['download_name'] = DB::name('user')->where('id = '.$data['download_id'])->value('truename');
			$data['download_time'] = date('Y-m-d H:i:s',$data['download_time']);
		}if(empty($data['cheng_id'])){
			$data['cheng_name'] = '暂无';
			$data['cheng_time'] = '暂无';
		}else{
			$data['cheng_name'] = DB::name('user')->where('id = '.$data['cheng_id'])->value('truename');
			$data['cheng_time'] = date('Y-m-d H:i:s',$data['cheng_time']);
		}
		$category = $this->getCategory();
		return $this->fetch('show',[
			'data'		=>	$data,
			'category'	=>	$category,
		]);
	}



	/*获取地址*/
	public function appendarg(){
		$prov_data = db::name('cn_prov')->field('code,name')->select();
		return $prov_data;
	}

	/*获取店铺信息*/
	public function getShopList()
	{
		$shopList = DB::name('shop')->field('id,title')->where('is_del = 0')->select();
		return $shopList;
	}

	/*获取用户等级*/
	public function getLevel()
	{
		$levelList = DB::name('customer_level')->field('id,title')->select();
		return $levelList;
	}

	/*获取品类*/
	public function getCategory()
	{
		$categoryList = DB::name('category')->field('id,title')->order('orderid asc')->select();
		return $categoryList;
	}

	/*获取详细地址*/
	public function  geAddress($prov,$city,$area)
	{
		$prov = DB::name('cn_prov')->where('code = '.$prov)->value('name');
		$city = DB::name('cn_city')->where('code = '.$city)->value('name');
		$area = DB::name('cn_area')->where('code = '.$area)->value('name');
		$add = $prov.$city.$area;
		return $add;
	}
	/*获取客户详细信息*/
	public function getCustomer($cid)
	{
		$cust = DB::name('customer')->where('id = '.$cid)->find();
		$dizhi = $this->geAddress($cust['province'],$cust['city'],$cust['area']);
		$cust['sex'] = $cust['sex'] == 1? '男' : '女' ;
		$cust['dizhi'] = $dizhi.$cust['address'];
		return $cust;
	}
	/*获取着装风格列表*/
	public function getStyle()
	{
		$style = DB::name('yifu_style')->where('status = 1')->order('id')->select();
		return $style;
	}		

	/*获取着装风格具体名字*/
	public function getStyleName($id)
	{
		$styleName = DB::name('yifu_style')->where('id = '.$id)->value('name');
		return $styleName;
	}

	/*ajax驳回订单*/
	public function gobackForAjax()
	{
		if(request::instance()->isPost())
		{
			$id = request::instance()->post('orderid');
			$data['reson'] = request::instance()->post('reaon');
			$data['check_id'] = Session::get('manage_id');
			$data['check_time'] = $data['update_time'] = time();
			$data['status'] = 'reject';
			$res = DB::name('order')->where('id = '.$id)->setField($data);
			if($res)
			{
				$this->success('驳回成功',url('index/index/index',array('con'=>'order','act'=>'index')));
			}else{
				$this->error('驳回失败');
			}

		}
	}

	/*ajax 驳回财审订单*/
	public function gobackForAjaxCai()
	{
		if(request::instance()->isPost())
		{
			$id = request::instance()->post('orderid');
			$data['reson'] = request::instance()->post('reaon');
			$data['caiwu_id'] = Session::get('manage_id');
			$data['caiwu_time'] = $data['update_time'] = time();
			$data['status'] = 'reject';
			$res = DB::name('order')->where('id = '.$id)->setField($data);
			if($res)
			{
				$this->success('驳回成功',url('index/index/index',array('con'=>'order','act'=>'index')));
			}else{
				$this->error('驳回失败');
			}

		}
	}

	public function ajaxDownload()
	{

		if(request::instance()->isPost())
		{
			$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'order_dow');
			if($auth != true){
				$re['msg'] = -1;
				return json($re);
			}
			$id = request::instance()->post('id');
			$order['download_id'] = Session::get('manage_id');
			$order['download_time'] = time();
			$order['status'] = 'download';
			$res = DB::name('order')->where('id ='.$id)->setField($order);
			if($res){
				$url = DB::name('order')->where('id = '.$id)->value('url');
				$re['url'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$url;
				$re['msg'] = 1;
			}else{
				$re['msg'] = 0;
			}
			return json($re);
		}
	}

	/*上传压缩包文件*/
	public function upload()
	{
		if(request::instance()->isPost()){
			$order_id = request::instance()->post('order_id');
			$file = request()->file("img");
				    // 移动到框架应用根目录/public/uploads/ 目录下
			if($file){
				$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
				if($info){
				            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
					$url =  $info->getSaveName();
					$url = 'public' . DS . 'uploads/'.$url;
					$data['url'] = $url;
					$data['upload_id'] = Session::get('manage_id');
					$data['update_time']  = $data['upload_time'] = time();
					$data['status'] = 'upload';
					$res = DB::name('order')->where('id = '.$order_id)->setField($data);
					if($res){
						$this->success('上传成功',url('index/index/index',array('con'=>'order','act'=>'index')));
					}
				}else{
				            // 上传失败获取错误信息
					echo $file->getError();
				}
			}
		}
	}

	/* 删除订单*/
	public function delorder()
	{
		if(Request::instance()->isPost())
		{
			$id = Request::instance()->post('id');
			/*判断权限*/
			$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'order_del');
			if($auth !=true){//没有删除菜单的权限
				$res['sta'] = '-1';
				$res['msg'] = '权限不足';
				
			}else{
				$del = DB::name('order')->where('id = '.$id)->setField('is_del',1);
				if($del){
					$res['sta'] = '1';
					$res['msg'] = '删除成功';
				}else{
					$res['sta'] = '0';
					$res['msg'] = '删除失败';
				}
			}
			return json($res);
		}
	}

	/*财审*/
	public function cai($id)
	{
		$authmenu = new \think\Authmenu();
		$uid = Session::get('manage_id');
		$auth=$authmenu->check($uid,'order_caiwu');
		if($auth != true){
			$this->error('权限不足');
			exit();
		}
		if(request::instance()->isPost())
		{
			$data['caiwu_id'] = Session::get('manage_id');
			$data['caiwu_time'] = time();
			$data['status'] = 'confirm';
			$res = DB::name('order')->where('id = '.$id)->setField($data);
			if($res)
			{
				$this->success('财务审核成功',url('index/index/index',array('con'=>'order','act'=>'index')));
			}
		}
		$data  = DB::name('order')->where('id = '.$id)->find();
		$data['customer'] = $this->getCustomer($data['cust_id']);
		$data['yifu_style'] = $this->getStyleName($data['yifu_style']);
		$category = $this->getCategory();
		return $this->fetch('cai',[
			'data'		=>	$data,
			'category'	=>	$category,
		]);
	}
	/*数据审核*/
	public function confirm($id)
	{
		//全局权限
		$authmenu = new \think\Authmenu();
		$uid = Session::get('manage_id');
		$auth=$authmenu->check($uid,'order_check');
		if($auth != true){
			$this->error('权限不足');
			exit();
		}
		if(request::instance()->isPost())
		{
			$data['check_id'] = Session::get('manage_id');
			$data['check_time'] = time();
			/*在这里判断下订单是不是其他工厂代加工 要是是的话订单状态该为完成  要是自己工厂的订单的话 状态为 待录入数据*/	
			$dai = DB::name('order')->where('id = '.$id)->value('is_dai');
			if($dai == 1){
			$data['status'] = 'success';//订单完成----修改于2018.07.19 
			}else{
			$data['status'] = 'input';//待录入数据
			}
			$res = DB::name('order')->where('id = '.$id)->setField($data);
			if($res)
			{
				$this->success('审核成功',url('index/index/index',array('con'=>'order','act'=>'index')));
			}
		}
		$data = DB::name('order')->where('id = '.$id)->find();
		$data['first'] = DB::name('order_first')->where('id ='.$data['first_id'])->find();
		$data['picarr'] = unserialize($data['first']['picarr']);
		$data['yifu_style'] = $this->getStyleName($data['yifu_style']);
		$data['customer'] = $this->getCustomer($data['cust_id']);
		$category = $this->getCategory();
		return $this->fetch('confirm',[
			'data'		=>	$data,
			'category'	=>	$category,
		]);
	}


	/*发货*/
	public function yundan()
	{
		if(request::instance()->isPost())
		{
			$data['yundan_num'] = request::instance()->post('yundan_num');
			$id = request::instance()->post('orderid');
			$data['status'] = 'success';
			$data['update_time'] = $data['fahuo_time'] = time();
			$res = DB::name('order')->where('id = '.$id)->setField($data);
			if($res){
				$this->success('发货成功',url('index/index/index',array('con'=>'order','act'=>'index')));
			}else{
				$this->error('发货失败');
			}

		}
	}

	/*ajax获取款式编号*/
	public function getStyleNum()
	{
		if(request::instance()->isPost())
		{
			$id = request::instance()->post('id');
			/*根据id查询品类的简称*/
			$value = DB::name('category')->where('id = '.$id)->value('value');
			/*获取当前订单的数量并加1 就等于现在订单的流水号*/
			$num = DB::name('order')->count()+1;
			if(strlen($num) == 1){
				$num = '000'.$num;
			}else if(strlen($num) == 2){
				$num = '00'.$num;
			}else if(strlen($num) == 3){
				$num = '0'.$num;
			}else{
				$num = $num;
			}
			$substr = $value.$num;
			/*返回拼接好的字符串*/
			return $substr;
		}
	}


	/*测试扫码枪功能*/
	public  function test()
	{
		$list = DB::name('order')
					->alias('a')
					->join('customer b','a.cust_id = b.id')
					->join('category c','a.category_id = c.id')
					->join('user d','a.user_id = d.id')
					->field('a.id,b.name,d.truename,c.title as pinlei,a.true_price,a.order_num,a.gongchang,FROM_UNIXTIME(a.add_time,"%Y-%m-%d %H:%i:%s") as add_time')
					->where('a.is_del = 0 and is_dai = 1 ')
					->order('gongchang,true_price')
					->select();
		var_dump(DB::name('order')->getLastSql()).die();
		//return $this->fetch();
	}
	/*ajax执行获取到扫码枪的信息之后的一系列操作*/
	public function testAjax()
	{
		if(request::instance()->isPost())
		{
			$data['value'] = request::instance()->post('order');
			$data['addtime'] = $update['addtime'] = time();
			$cunzai = DB::name('test')->field('id,num')->where("value = '".$data['value']."'")->find();
			if(count($cunzai) >0 ){//说明已经存在
				$update['num'] = $cunzai['num']+1;

				$res = DB::name('test')->where("value = '".$data['value']."'")->setField($update);
			}else{
			$data['num'] = 1;
			$res = DB::name('test')->insert($data);
			}
			if($res)
			{
				$rs['msg'] = '录入成功';
				$rs['sus'] = '1';
			}
			return json($rs);
		}
	}

	/*批量导出数据*/
	public function excel()
	{
		$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'order_daochu');
			if($auth != true){
				$this->error('权限不足');
		}
		//$where['id'] = $update['id']= array('in',$attr);
		
			$name='定制订单列表';
			/*客户 日期 价格 订单属性  客户来源 新老客户 顾问  所属城市*/
			$header=['序号','客户','日期','品类','价格','订单属性','客户来源','新老客户','顾问','所属店铺'];
			$where['is_del'] = 0;
			/*获取到id 然后查询这些id所对应的信息 最后放在表格里面*/
			$list = DB::name('order')->where($where)->order('id desc')->select();
			$str = array();
			foreach ($list as $k => $v) {
				$customer = DB::name('customer')->where('id ='.$v['cust_id'])->find();//客户
				$v['customer'] = $customer['name'];
				switch ($customer['from']) {
					case '1':
						$v['from'] = '个人查找';
						break;
					case '2':
						$v['from'] = '老客户转介绍';
						break;
					case '3':
						$v['from'] = '公司派单';
						break;
				}

				
				$v['is_new'] = $customer['is_New'] == 1 ? '新用户' : '老用户' ;
				$v['addtime'] = date('Y-m-d H:i:s',$v['add_time']);
				$v['shuxing'] = $v['shuxing']==1 ? '正常订单': '售后订单' ;
				$v['pinlei'] = DB::name('category')->where('id ='.$v['category_id'])->value('title');
				$v['user'] = DB::name('user')->where('id = '.$v['user_id'])->value('truename');
				$v['shop'] = DB::name('shop')->where('id = '.$v['shop_id'])->value('title');
				$str[] = [$v['id'],$v['customer'],$v['addtime'],$v['pinlei'],$v['true_price'],$v['shuxing'],$v['from'],$v['is_new'],$v['user'],$v['shop']];
			}
			$data=$str;
			excelExport($name,$header,$data);
	}

}
?>